package org.achao.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author achao
 * @description 全局异常捕获
 * @date 2023/10/10 22:23
 */
@ControllerAdvice
public class MyExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value =Exception.class)
    public String exceptionHandler(Exception e){
        System.out.println("全局异常捕获>>>:"+e);
        return "全局异常捕获,错误原因>>>"+e.getMessage();
    }

    @ResponseBody
    @ExceptionHandler(value =RuntimeException.class)
    public String runtimeExceptionHandler(RuntimeException e){
        System.out.println("运行时异常捕获>>>:"+e);
        return "运行时异常捕获,错误原因>>>"+e.getMessage();
    }

    // @ResponseBody
    // @ExceptionHandler(value =MyException.class)
    // public String myExceptionHandler(MyException e){
    //     System.out.println("自定义异常捕获>>>:"+e);
    //     return "自定义异常捕获,错误原因>>>"+e.getMessage();
    // }

}
